<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 50%;
            margin: 0px auto;
            padding: 20px;
            display: flex;
        }

        .box_left {
            flex: 1;
            border: 3px solid #ddd;
            padding: 10px;
            /* background-color: red; */

        }

        ul {
            list-style: none;
            list-style: none;
            display: flex;
            width: 200px;
            flex-direction: column;
        }

        li {
            height: 30px;
            border: solid 2px #e67e22;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-left: 10px;
            color: #333;
            transition: 1s;
        }

        .box_left li a {
            border-radius: 3px;
            width: 20px;
            height: 20px;
            text-decoration: none;
            text-align: center;
            background: #16a085;
            color: white;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 5px;

        }

        .box_right {
            flex: 1;
            background-color: green;
            border: 3px solid #ddd;
        }

        p {
            margin-top: 20px;
            text-align: center;
            font-size: 15px;
            color: #fff;
        }

        #warp {
            margin-top: 15px;
        }

        #warp span {
            padding: 5px;
            background-color: #fff;
            color: #16a085;
            margin: 5px;
            border-radius: 5px;
        }

        .remove {
            border: solid 2px #eee;
            opacity: 0.8;
            color: #eee;
        }

        .remove a {
            background: #eee;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box_left">
            <ul>
                <li><span>jsp</span><a href="#">+</a></li>
                <li><span>js</span><a href="#">+</a></li>
                <li><span>css</span><a href="#">+</a></li>
            </ul>
        </div>
        <div class="box_right">
            <p id="count">共选了几门课</p>
            <div id="warp"></div>
        </div>
    </div>
</body>
<script>
    class clist {
        constructor() {
            this.lists = document.querySelectorAll(".box_left li");
            this.count = document.getElementById('count');
            this.warp = document.getElementById('warp');
            this.weakMap = new WeakMap();
            this.run();
        }
        run() {
            this.lists.forEach(item => {
                item.querySelector('a').addEventListener('click', e => {
                    const elem = e.target;
                    const state = elem.getAttribute('select');
                    if (state) {//判断是否选中  选中再次点击 取消
                        elem.removeAttribute('select');
                        this.weakMap.delete(elem.parentElement);
                        elem.innerHTML = '+';
                        elem.style.backgroundColor = 'green';
                    } else {//未选中 第一次点击  选中 添加到weakmap中 
                        elem.setAttribute('select', true);
                        this.weakMap.set(elem.parentElement, true);
                        elem.innerHTML = '-';
                        elem.style.backgroundColor = 'red';
                    }
                    this.render();


                });
            });
        }
        countNum() {
            return [...this.lists].reduce((count, item) => {
                return count += this.weakMap.has(item) ? 1 : 0;
            }, 0);
        }
        xlist() {
            return [...this.lists].filter(item => {
                return this.weakMap.has(item);
            }).map(item => {
                return `<span>${item.querySelector('span').innerHTML}</span>`;
            });
        }
        render() {
            this.count.innerHTML = `共选了${this.countNum()}课`;
            this.warp.innerHTML = this.xlist().join('');
        }
    }
    new clist();
</script>

</html>